
import { gameObject } from "./gameObject";
import { cell } from "./cell";

export class snake extends gameObject {
    constructor(info, gameMap) {
        super();
        this.id = info.id;
        this.color = info.color;
        this.gameMap = gameMap;

        this.cells = [new cell(info.r, info.c)];//存放蛇的身体 cells[0]是头
        this.nextCell = null;//蛇头下次到达的xy

        this.speed = 5;//每秒走5个格子
        this.direction = -1;//0123上右下左
        this.status = "idle"//静止,move表示正在移动,die已经死亡

        this.dr = [-1, 0, 1, 0]; // 行
        this.dc = [0, 1, 0, -1]; //列

        this.step = 0;

        this.eps = 1e-2;

        this.eyeDirection = 0;
        if (this.id === 1) this.eyeDirection = 2;

        this.eye_dx = [  // 蛇眼睛不同方向的x的偏移量
            [-1, 1],
            [1, 1],
            [1, -1],
            [-1, -1],
        ];
        this.eye_dy = [  // 蛇眼睛不同方向的y的偏移量
            [-1, -1],
            [-1, 1],
            [1, 1],
            [1, -1],
        ]

    }


    start() {

    }
    checkTail() {//蛇尾
        if (this.step <= 10) return true;
        if (this.step % 3 === 1) return true;//不变长
        return false;//不变长
    }
    setDirection(d) {
        this.direction = d;
    }

    nextStep() { //变到下一回合
        const d = this.direction;
        this.nextCell = new cell(this.cells[0].r + this.dr[d], this.cells[0].c + this.dc[d]);
        this.direction = -1;
        this.status = "move";
        this.step++;

        this.eyeDirection = d;

        // 求长度
        const k = this.cells.length;
        for (let i = k; i > 0; i--) { // 初始元素不变 每一个元素往后移动一位
            this.cells[i] = JSON.parse(JSON.stringify(this.cells[i - 1]));
        }

        // if (!this.gameMap.checkVaild(this.nextCell))
        //     this.status = 'die';//撞到了死掉
    }

    updateMove() {
        const dx = this.nextCell.x - this.cells[0].x;
        const dy = this.nextCell.y - this.cells[0].y;
        let distance = Math.sqrt(dx * dx + dy * dy);
        if (distance < this.eps) {  // 走到目标点了
            this.cells[0] = this.nextCell;  // 添加一个新蛇头
            this.next_cell = null;
            this.status = "idle";  // 走完了，停下来
            if (!this.checkTail()) {  // 蛇不变长
                this.cells.pop();
            }
        } else {
            const move_distance = this.speed * this.timedelta / 1000;
            this.cells[0].x += move_distance * dx / distance;
            this.cells[0].y += move_distance * dy / distance;
            if (!this.checkTail()) {
                const k = this.cells.length;
                const tail = this.cells[k - 1], tail_target = this.cells[k - 2];
                const tail_dx = tail_target.x - tail.x;
                const tail_dy = tail_target.y - tail.y;
                tail.x += move_distance * tail_dx / distance;
                tail.y += move_distance * tail_dy / distance;
                //console.log("蛇的长度"+k);
            }
        }
    }

    update() {  // 每一帧执行一次
        if (this.status === 'move') {
            this.updateMove();
        }
        this.render();
    }

    render() {//画蛇出来
        const L = this.gameMap.L;
        const ctx = this.gameMap.ctx;

        ctx.fillStyle = this.color;

        if (this.status === 'die')
            ctx.fillStyle = 'white';

        for (const cell of this.cells) {//of遍历的是数组内部值
            ctx.beginPath();
            ctx.arc(cell.x * L, cell.y * L, L / 2 * 0.8, 0, Math.PI * 2);
            ctx.fill();
        }
        for (let i = 1; i < this.cells.length; i++) {
            const a = this.cells[i - 1], b = this.cells[i];
            if (Math.abs(a.x - b.x) < this.eps && Math.abs(a.y - b.y) < this.eps)
                continue;
            if (Math.abs(a.x - b.x) < this.eps) {
                ctx.fillRect((a.x - 0.4) * L, Math.min(a.y, b.y) * L, L * 0.8, Math.abs(a.y - b.y) * L);
            } else {
                ctx.fillRect(Math.min(a.x, b.x) * L, (a.y - 0.4) * L, Math.abs(a.x - b.x) * L, L * 0.8);
            }
        }

        ctx.fillStyle = "black"; this.eye_dx = [  // 蛇眼睛不同方向的x的偏移量
            [-1, 1],
            [1, 1],
            [1, -1],
            [-1, -1],
        ];
        this.eye_dy = [  // 蛇眼睛不同方向的y的偏移量
            [-1, -1],
            [-1, 1],
            [1, 1],
            [1, -1],
        ]

        for (let i = 0; i < 2; i++) {
            const eye_x = (this.cells[0].x + this.eye_dx[this.eyeDirection][i] * 0.15) * L;
            const eye_y = (this.cells[0].y + this.eye_dy[this.eyeDirection][i] * 0.15) * L;
            ctx.beginPath();
            ctx.arc(eye_x, eye_y, L * 0.05, 0, Math.PI * 2);
            ctx.fill();
        }

    }
}